﻿/** 
 * Twi Framework Core 
 * @Author：feitianxinhong http://xuzhihong1987.blog.163.com/  
 * @Version：3.0.0
 * @CreateDate：2015-11-20
 * @LastEditDate：2015-11-20
 * @PublicDate：
 * @Require: jQuery
 * @Example: Visit http://blog.163.com/twi_fly  
 * @Copyright ©2010-2015 杭州踏影科技有限公司 http://www.tyingsoft.com 
 * @Remark：Tyingsoft之Twilight/Twifly开源系列. 转载请保留本行注明.   
 * @License：Twi Framework of Tyingsoft is licensed under the MIT license.  
 * @Description：v3.0.0版本重新整理，全新改版
 *    因Twi.Ajax方法内用到:Twi.Msg、Twi.LoadingMask 。这两个插件在此js之后引入即可。
 * ------------------------------------------------------------------
 *  编号    版本号      作者               修改日期        修改内容
 * ------------------------------------------------------------------
 *   1     3.0.0       feitianxinhong    2015-11-20
 *   2
 * ------------------------------------------------------------------
 */

(function ($) {
    var global = this;

    if (typeof Twi === 'undefined') {
        global.Twi = {};
    }
    Twi.global = global;


    /**
     * 空函数
     */
    Twi.emptyFn = function () { }

    /**
	* PageHandler路径
	*/
    Twi.Url = "/PageHandler.ashx";

    /**
	*config:$.ajax(config)的参数
	*config.data:参数配置
	*config.success:（做了些改进）  twiReturn.success == true时执行的函数
	*/
    Twi.Ajax = function (config) {
        var ajaxConfig = $.extend({
            url: Twi.Url,
            dataType: 'JSON',
            type: 'POST',
            loadingMask: false,          //提交时是否自动加载Twi.LoadingMask...
            maskMsg: '正在处理请稍后...',
            errorMsgTarget: 'body',  //错误信息显示的位置，默认：body 。 ID需带井号，如：#id
            emptyMsg: false           //显示错误信息前是否清空内容
        }, config);

        /*TwiLoadingMask*/
        var twiMask;
        if (ajaxConfig.loadingMask) {
            twiMask = new Twi.LoadingMask({
                msg: ajaxConfig.maskMsg
            });
            twiMask.show();
        }
        delete ajaxConfig.loadMask;
        delete ajaxConfig.maskMsg;

        /*ajax请求成功方法*/
        ajaxConfig.success = function (twiReturn, textStatus, jqXHR) {
            if (twiReturn.success) {
                if (typeof config.success == "function") config.success(twiReturn);
            }
            else {
                if (Twi.Msg) {
                    Twi.Msg.Error(twiReturn.message);
                }
                else {
                    if (ajaxConfig.emptyMsg) { $(ajaxConfig.errorMsgTarget).emptyMsg(); }
                    $(ajaxConfig.errorMsgTarget).append('<div class="alert alert-warning alert-dismissable"><button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button><strong>温馨提示：</strong>' + twiReturn.errors + '</div>');
                }
                if (twiReturn.knowError == "102") {
                    if (typeof Twi.QuickReLogin == 'function') {
                        //快速重登录
                        Twi.QuickReLogin();
                    }
                    else {
                        location.href = "/Login/Index";
                    }
                }
            }
        }

        /*ajax请求最后执行的方法*/
        ajaxConfig.complete = function () {
            if (typeof config.complete == "function") config.complete();
            if (twiMask) { twiMask.hide(); }
        }

        $.ajax(ajaxConfig);
    }

    Twi.QuickReLogin = function () {
        //QRL: QuickReLogin
        if ($('#qrlContainer').length == 0) {
            $("body").append('<div id="qrlContainer"></div>');
            $("#qrlContainer").load('/Login/QuickReLogin');
        }
    };

    /**
	* Grid一页显示的记录条数
	*/
    Twi.PageSize = 15;

    /**
	*开发人员才能维护基础数据，发布的时候，或是给测试的时候，这个应设为false
	*/
    Twi.IsDeveloper = false;
    Twi.NotDeveloperError = "系统基础支撑数据，只能由开发人员维护，</br>否则系误操作可能会引起系统对应功能无法操作！";
    /**
	*获取地址栏参数
	*/
    Twi.GetUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }


    /**
	 *Cookies操作
	 */
    Twi.Cookies = {};
    Twi.Cookies.set = function (name, value) { var argv = arguments; var argc = arguments.length; var expires = (argc > 2) ? argv[2] : null; var path = (argc > 3) ? argv[3] : '/'; var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false; document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "") };
    Twi.Cookies.get = function (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; var j = 0; while (i < clen) { j = i + alen; if (document.cookie.substring(i, j) == arg) return Twi.Cookies.getCookieVal(j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break } return null };
    Twi.Cookies.clear = function (name) { if (Twi.Cookies.get(name)) { document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" } };
    Twi.Cookies.getCookieVal = function (offset) { var endstr = document.cookie.indexOf(";", offset); if (endstr == -1) { endstr = document.cookie.length } return unescape(document.cookie.substring(offset, endstr)) };

    /*当前登录用户*/
    Twi.CurrentUser = {
        FID: Twi.Cookies.get('Twi_User_FID'),
        FREALNAME: Twi.Cookies.get('Twi_User_FREALNAME'),
        FNAME: Twi.Cookies.get('Twi_User_FNAME'),
        PHONENUMBER: Twi.Cookies.get('Twi_User_PHONENUMBER'),
        PARKCODE: Twi.Cookies.get('Twi_User_PARKCODE'),
        STORAGECODE: Twi.Cookies.get('Twi_User_STORAGECODE')
    };

    Twi.ClearUserCookies = function () {
        Twi.Cookies.clear("Twi_User_FID");
        Twi.Cookies.clear("Twi_User_FREALNAME");
        // Cookies.clear("Twi_User_FNAME");
        Twi.CurrentUser.FID = "";
        Twi.CurrentUser.FREALNAME = "";
        Twi.CurrentUser.FNAME = "";
        Twi.CurrentUser.PHONENUMBER = "";
        Twi.CurrentUser.PARKCODE = "";
        Twi.CurrentUser.STORAGECODE = "";
    }


})(window.jQuery);